// 用于安卓苹果归因上报
import request from "./request.js";
import config from "./config.js";
import util from "./util.js";
export default {
	
	// 启动App
	startApp(){
		let _this = this;
		return;
		if(config.osName === 1){
			_this.iosAdsGetToken().then((token) => {
				_this.startPost({ iadToken: token });
			});
		}else if(config.osName === 2){
			_this.getOAID().then((e) => {
				_this.startPost();
			});
		}
	},
	
	// 启动App时上报
	startPost(data = {}){
		request.ajax({
			url: "/api/ascribe/start_up",
			method: "POST",
			data: data,
			success: (res) => {},
			error: (err) => {}
		});
	},
	
	// iOS获取归因Token
	iosAdsGetToken(){
		// #ifdef APP-PLUS
		let _this = this;
		return new Promise((resolve) => {
			if (config.osName === 1) {
				let AdsTool = uni.requireNativePlugin("TranscriptsPlugin-AdsTool");
				let adsToken = "";
				try {
					adsToken = AdsTool.getToken() ? AdsTool.getToken().token : "";
				} catch (_) { }
				
				resolve(adsToken ? adsToken : "App: token为空，可能是因为iOS14.3以下");
			}else{
				resolve("安卓无法获取iOS的归因Token");
			}
		});
		// #endif
	},
	
	// 获取OAID
	getOAID() {
		let _this = this;
		return new Promise((resolve) => {
			let oaid = uni.getStorageSync("oaid");
			if((oaid != "" && oaid != undefined && oaid != null) && (oaid != 0 && oaid != "0")){
				resolve(oaid);
				return;
			}
	
			// #ifdef APP-PLUS
			if (config.osName === 1) {
				uni.setStorageSync("oaid", "0");
				resolve("0");
			} else {
				if (plus.runtime.isAgreePrivacy()) {
					const oaidTool = uni.requireNativePlugin("oaid-tool");
					oaidTool.getOAID((str) => {
						uni.setStorageSync("oaid", str);
						resolve(str);
					});
				} else {
					uni.setStorageSync("oaid", "0");
					resolve("0");
				}
			}
			return;
			// #endif
	
			// #ifndef APP-PLUS
			uni.setStorageSync("oaid", "0");
			resolve("0");
			return;
			// #endif
		});
	},
	
};